<?php /* УТФ-8:fix */

class SPIcontroller__profiles__front extends SPIcontroller__profiles
{
    public function exec( & $content )
    {
        $r = array();

        $content['active_topic'] = $content['env_topic'];

        switch( $content['env_topic'] )
        {
            case 'leaders':
                $select_type = array( 'dj', 'journalist' );
                break;

            case 'dj':
                $select_type = 'dj';
                break;

            case 'journalist':
                $select_type = 'journalist';
                break;

            default:
                return false;
        }

        //-----------------------------------------------------------------------------------------
        // Details list

        if( $content['env_vars']['i'] )
        {
            $content['topic_template_filename'] = 'leaders/details.tpl';

            $r['details'] = SPIm::I('profiles')->front->get_by_id( $content['env_vars']['i'] );

                $r['details']['preview'] = SPIm::I('media')->get_public_sourceByIndexNewJpeg(
                $this->MEDIA_GID, $r['details']['id'], 1,
                array( 'mode' => 'preview' )
            );

            // ----------------------------------------------------------------
            // Self TOP

            $qres = SPIm::I('dj_online')->front->select_dj_tops( $r['details']['id'] );

            while( $topitem = $qres->fetch() )
            {
                $topitem['preview'] = SPIm::I('media')->get_public_sourceByIndexNewJpeg(
                    SPIc::I('songnvideo')->MEDIA_PHOTO_GID, $topitem['pid_content'], 1,
                    array( 'mode' => 'preview' )
                );

                $topitem['sound'] = SPIm::I('media')->get_public_sourceByIndexOld(
                    SPIc::I('songnvideo')->MEDIA_AUDIO_GID, $topitem['pid_content'], 1
                );

                $r['details']['dj_top'][] = $topitem;
            }
            $qres->close();

            // ----------------------------------------------------------------

            $r['details']['media_photos'] = array();

            $qres = SPIm::I('media')->select( $this->MEDIA_GALLERY_GID, $r['details']['id'] );

            while( $gitem = $qres->fetch() )
            {
                $gitem['preview'] = SPIm::I('media')->get_public_source_by_media_item(
                    $gitem,
                    array( 'mode' => 'preview' )
                );

                $r['details']['media_photos'][] = $gitem;
            }
            $qres->close();

            // ----------------------------------------------------------------
        }
        else
        {
            $content['topic_template_filename'] = 'leaders/list.tpl';
        }

        //-----------------------------------------------------------------------------------------
        // Content list

        $select_types = is_array( $select_type ) ? $select_type : array( $select_type );

        foreach( $select_types as $select_type )
        {
            $qres = SPIm::I('profiles')->front->select( $select_type );

            while( $item = $qres->fetch() )
            {
                if( $item['id'] == $content['env_vars']['i'] )
                {
                    continue; // Ignore details-view item
                }

                $item['preview'] = SPIm::I('media')->get_public_sourceByIndexNewJpeg(
                    $this->MEDIA_GID, $item['id'], 1,
                    array( 'mode' => 'preview' )
                );

                if( $item['pubdate'] )
                {
                    $item['date'] = strftime( '%d.%m.%Y', strtotime( $item['pubdate'] ) );
                }

                $item['link'] = SPIc::I('front')->link->profile( $select_type, $item['id'], $item['fname'].' '.$item['lname'] );

                $r['items'][$select_type][] = $item;
            }
            $qres->close();
        }

        $content['leaders'] = $r;
        return true;
    }
}
